package com.example.demo.event.base;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;

/**
 * 事件驱动基类
 */
public abstract class AbstractSimpleEventListener<T extends Serializable> {

    protected Class<T> cls;

    @SuppressWarnings("unchecked")
	public AbstractSimpleEventListener() {
        cls = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }

    public abstract void exec(EventSource<T> source);

    /**
     * 执行事件本身
     * @param event 事件对象
     */
    @SuppressWarnings("unchecked")
	public void doEvent(Event<T> event) {
        EventSource<T> source = (EventSource<T>) event.getSource();
        if (source.getSource().getClass() == cls) {
            exec(source);
        }
    }
}
